/* MedikamentSuchen.java
 *
 * @author Medisoft (Team D)
 * @date 15.05.2008
 *
 * MedikamentSuchen Klasse
 */

package ocumed.teams.teamd.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import ocumed.teams.teamd.application.MedikamentSuchenTableModel;

/**
 * Ocumed
 * 
 * @author Medisoft (Team D)
 * @version 1.0
 * @see Object
 */
public class MedikamentSuchen extends javax.swing.JFrame implements MouseListener,
		WindowListener, ComponentListener, KeyListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTable jTable1;

	private PanUntersuchung m_frm;
	private JScrollPane jScrollPane1;
	private ArrayList<JTextField> m_filters = new ArrayList<JTextField>();
	private MedikamentSuchenTableModel m_model;
	
	public MedikamentSuchen(PanUntersuchung frm) {
		super();

		m_model = new MedikamentSuchenTableModel();

		initGUI();


		addFilterButtons();

		m_frm = frm;

		jTable1.setAutoCreateColumnsFromModel(false);
		jTable1.setCellSelectionEnabled(false);
		jTable1.setRowSelectionAllowed(true);

		jTable1.getTableHeader().setReorderingAllowed(false);

		

	}

	private void addFilterButtons() {

		// id column (letzte) removen
		jTable1.removeColumn(jTable1.getColumnModel().getColumn(jTable1.getColumnModel().getColumnCount()-1));
		
		for (int i = 0; i < jTable1.getColumnModel().getColumnCount(); i++) {
			JTextField f = new JTextField();

			f.setName("col_" + i);
			f.addKeyListener(this);
			m_filters.add(f);
			add(f);
		}

	}

	private void resizeFilterButtons() {
		int pos = 0;
		int i = 0;

		jScrollPane1.setBounds(0, 30, getWidth()-15, getHeight()-55);
		jScrollPane1.validate();
		
		for (JTextField f : m_filters) {

			TableColumn c = jTable1.getColumnModel().getColumn(i);
			int prefSize = c.getWidth();

			f.setBounds(pos, 0, prefSize, 20);

			pos = pos + prefSize;
			add(f);
			i++;
		}
	}

	private void initGUI() {
		try {

			this.addComponentListener(this);
			this.addWindowListener(this);

			// setLayout(new GridLayout(1, 1));

			jTable1 = new JTable();
			jTable1.setModel(m_model);

			jTable1.addMouseListener(this);

			jScrollPane1 = new JScrollPane();
			jScrollPane1.setPreferredSize(new Dimension(320, 240));
			jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			
			jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			
			jScrollPane1.getViewport().add(jTable1, BorderLayout.CENTER);
			add(jScrollPane1);


			this.setSize(600, 600);
			this.setLocationRelativeTo(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getClickCount() > 1) {
			int row = jTable1.getSelectedRow();
			MedikamentSuchenTableModel dtm = (MedikamentSuchenTableModel) jTable1
					.getModel();

			// in der letzten spalte die ID
			int id = (Integer) dtm.getId(row);

			m_frm.addMedikament(id);

			this.setVisible(false);
		}

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentResized(ComponentEvent arg0) {
		resizeFilterButtons();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		JTextField f = (JTextField) arg0.getSource();

		Integer col = Integer.valueOf(f.getName().replace("col_", ""));
		String filter = f.getText().toUpperCase();

		m_model.setFilter(filter, col);
		jTable1.updateUI();

	}

	@Override
	public void keyTyped(KeyEvent arg0) {

	}

}
